let db = require("../db/db")

let handle = async function (action, data, login_info, req, res) {
	switch (action) {
		case "info":
			let pid = data.id;
			let user_id = login_info.user_id;
			let sql = "select * from sys_function where page_id=? and is_deleted=0 and \
				id in (select func_id from sys_role_func_r where is_deleted=0 and \
				role_id in (select role_id from sys_user_role_r where is_deleted=0 and user_id=?))";
			let result = await db.query(sql, [pid, user_id]);
			let rows = result.rows;
			let rights = [];
			for (let row of rows) {
				rights.push(row.name);
			}
			if (!rights.includes("访问")) {
				return Error('无访问此page的权限');
			}
			sql = "select p.*,t.js from sys_page p left outer join sys_page_type t on p.page_type_id=t.id where p.id=? and p.is_deleted=0";
			result = await db.query(sql, [pid]);
			rows = result.rows;
			if (rows.length == 0) { return Error('不存在这个page,id:' + id); }
			let row = rows[0];
			row.property = JSON.parse(row.property);
			row.rights = rights;
			row.login_info = login_info;
			return row;
			break;
		default:
			return Error('未知的action:' + action);
	}
}

module.exports = { handle };